<?php

namespace App\Http\Controllers\H5;

use App\Events\OrderEvent;
use App\Library\KdClient;
use App\Library\Mall\BoughtTrait;
use App\Models\Express;
use App\Models\ItemReviews;
use App\Models\RefundReason;
use App\WechatMessages\ZPH\OrderRefundCreatedMessage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class BoughtController extends BaseController
{

    use BoughtTrait;

    public function index()
    {

        $tradeCode = $this->request->input('tradeCode');
        return $this->view('h5.bought.bought', compact('tradeCode'));
    }

    public function detail()
    {
        $order_id = $this->request->input('order_id');
        $order = $this->user()->boughts()->find($order_id);

        if ($order)
        {
            $shipping = $order->shipping;
            $items = $order->items;
            $shop = $order->shop;
            return $this->view('h5.bought.detail', compact('order_id', 'order', 'shipping', 'items', 'shop'));
        } else {
            return ajaxError(404, 'order not found');
        }
    }

    public function shipping()
    {
        return $this->view('h5.bought.shipping');
    }

    public function getShipping()
    {
        $order_id = $this->request->input('order_id');
        $order = $this->user()->boughts()->find($order_id);

        if ($order)
        {
            $shipping = $order->shipping;
            $express = $shipping->express;
            if ($express)
            {
                $clinet = new KdClient();
                $clinet->setOrderCode($order->order_no);
                $clinet->setShipperCode($express->code);
                $clinet->setLogisticCode($shipping->express_no);
                $res = $clinet->sendRequest();

                return ajaxReturn([
                    'express_name'=>$express->name,
                    'express_no'=>$shipping->express_no,
                    'traces'=>$res->getTraces()
                ]);
            } else {
                return ajaxError(1, '未找到物流信息');
            }
        } else {
            return ajaxError(404, trans('mall.order does not exist'));
        }
    }

    public function requestRefund(Request $request)
    {
        $order_id = $request->input('order_id', 0);
        $this->user()->boughts()->find($order_id)->update([
            'refund_status'=>1,
            'trade_status'=>5,
            'refund_at'=>time()
        ]);
        return ajaxReturn();
    }

    public function refund()
    {
        $order_id = $this->request->input('order_id');
        $order = $this->user()->boughts()->find($order_id);

        $reasons = RefundReason::orderBy('displayorder')->get();
        $expresses = Express::all();

        $redirect = back()->getTargetUrl();

        $this->assign(compact('order_id', 'order', 'reasons', 'expresses', 'redirect'));
        $refund = $order->refund;
        if ($refund)
        {
            return $this->view('h5.bought.refund_detail', compact('refund'));
        } else {
            return $this->view('h5.bought.refund');
        }
    }

    public function applyRefund()
    {
        $order_id = $this->request->input('order_id');
        $refund_reason = $this->request->input('refund_reason');
        $refund_amount = $this->request->input('refund_amount');
        $buyer_message = $this->request->input('buyer_message');
        $refund_express_id = $this->request->input('refund_express_id');
        $refund_express_no = $this->request->input('refund_express_no');
        $images = serialize($this->request->input('images', []));

        $order = $this->user()->boughts()->find($order_id);
        if (!$order->refund)
        {
            $refund_no = createReundNo();
            $order->changeToRefunding();
            $order->refund = $order->refund()->create(compact('refund_no', 'refund_reason', 'refund_amount',
                'buyer_message', 'refund_express_id','refund_express_no','images'));
            event(new OrderEvent($order, 'refunding'));
        }

        return ajaxReturn();
    }

    public function cancelRefund(Request $request)
    {
        $order_id = $request->input('order_id');
        $order = $this->user()->boughts()->find($order_id);
        if ($order)
        {
            if ($order->pay_status)
            {
                $order->trade_status = 2;
            }

            if ($order->shipping_status)
            {
                $order->trade_status = 3;
            }

            if ($order->receive_status)
            {
                $order->trade_status = 4;
            }
            $order->refund_status = 0;
            $order->save();

            $order->refund()->delete();
        }

        return ajaxReturn();
    }

    public function resetRefund(Request $request)
    {

        $order_id = $this->request->input('order_id');
        $refund_reason = $this->request->input('refund_reason');
        $refund_amount = $this->request->input('refund_amount');
        $buyer_message = $this->request->input('buyer_message');
        $refund_express_id = $this->request->input('refund_express_id');
        $refund_express_no = $this->request->input('refund_express_no');
        $images = serialize($this->request->input('images', []));

        $order = $this->user()->boughts()->find($order_id);
        if ($order->refund)
        {
            $order->changeToRefunding();

            $refund = $order->refund;
            $refund->refund_status = 0;
            if ($refund_reason)
            {
                $refund->refund_reason = $refund_reason;
            }

            if ($refund_amount)
            {
                $refund->refund_amount = $refund_amount;
            }

            if ($buyer_message)
            {
                $refund->buyer_message = $buyer_message;
            }

            if ($refund_express_id)
            {
                $refund->refund_express_id = $refund_express_id;
            }

            if ($refund_express_no)
            {
                $refund->refund_express_no = $refund_express_no;
            }

            if ($images)
            {
                $refund->images = $images;
            }
            $refund->save();

            event(new OrderEvent($order, 'refunding'));
        }

        return ajaxReturn();
    }

    public function showEvaluate(Request $request)
    {
        $order_id = $request->input('order_id');
        $order = $this->user()->boughts()->find($order_id);
        $item = $order->items()->first();


        return $this->view('h5.bought.evaluate', compact('order', 'order_id', 'item'));
    }

    public function showAddReview(Request $request)
    {
        $order_id = $request->input('order_id');
        $order = $this->user()->boughts()->find($order_id);

        return $this->view('h5.bought.addreview', compact('order', 'order_id'));
    }

    public function addReview(Request $request)
    {
        $order_id = $request->post('order_id');
        $message  = $request->post('message');
        $images   = $request->post('images', []);

        $order = $this->user()->boughts()->find($order_id);
        if ($order)
        {
            $order->items()->get()->map(function ($item) use ($order_id, $message, $images){
                ItemReviews::where('itemid', $item->itemid)
                    ->where('uid', $this->uid)
                    ->where('order_id', $order_id)
                    ->update([
                        'add_message'=>$message,
                        'add_images'=>serialize($images),
                        'add_image_count'=>count($images),
                        'add_at'=>time()
                    ]);
            });
            $order->buyer_rate = 2;
            $order->save();
        }
        return ajaxReturn();
    }
}
